
package demo.Thread;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class clientThread02 {
    public static void main(String[] args) {
        String clientName = "Thread-02";
        Thread receiveingFromServicer = new Thread(){

            @Override
            public void run() {

                Socket s = null;

                InputStream in = null;
                try {
                    s = new Socket(InetAddress.getByName("192.168.201.1"),22222);//用户读取连接

                    in = s.getInputStream();

                    int len;
                    byte[] bys = new byte[8192];

                    while ((len = in.read(bys))!=-1){
                        String line = new String(bys,0,len);
                        if (line.startsWith(clientName)){
                            continue;
                        }
                        System.out.print(line);//todo
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    if (s != null) {
                        try {
                            s.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }

            }
        };

        Thread sendToServicer = new Thread(){
            @Override
            public void run() {
                Socket s = null;

                InputStream in= null;

                OutputStream out = null;//发送聊天记录

                try {
                    s = new Socket(InetAddress.getByName("192.168.201.1"),22223);//客户输出连接

                    out = s.getOutputStream();

                    in = System.in;

                    int len;
                    byte[] bys = new byte[8192];

                    while((len = in.read(bys))!=-1) {
                        String line = new String(bys,0,len);
                        out.write((clientName+":"+line).getBytes());
                        out.flush();
                    }


                } catch (IOException ex) {
                    ex.printStackTrace();
                }finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

                    if (s != null) {
                        try {
                            s.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }


            }

        };
        
        receiveingFromServicer.start();
        sendToServicer.start();

    }
}

